package com.jinzecloud.reportapproval.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jinzecloud.reportapproval.bean.SoilFirmnessMoisture;
import com.jinzecloud.reportapproval.bean.VegetationRecordForm;
import com.jinzecloud.reportapproval.dto.AuditParam;
import com.jinzecloud.reportapproval.dto.DataStagingParam;
import com.jinzecloud.reportapproval.enums.StateEnum;
import com.jinzecloud.reportapproval.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import com.jinzecloud.reportapproval.bean.SpeedOperationTable;
import com.jinzecloud.reportapproval.service.SpeedOperationTableService;
import com.jinzecloud.reportapproval.utils.PageUtils;
import com.jinzecloud.reportapproval.utils.R;



/**
 *
 *
 * @author æ­å°
 * @email 1971240051@qq.com
 * @date 2024-12-03 18:28:40
 */
@RestController
@RequestMapping("reportapproval/speedoperationtable")
public class SpeedOperationTableController {
    @Autowired
    private SpeedOperationTableService speedOperationTableService;

    @Autowired
    private AdminService adminService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("reportapproval:speedoperationtable:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = speedOperationTableService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("reportapproval:speedoperationtable:info")
    public R info(@PathVariable("id") Integer id){
		SpeedOperationTable speedOperationTable = speedOperationTableService.getById(id);

        return R.ok().put("speedOperationTable", speedOperationTable);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("reportapproval:speedoperationtable:save")
    public R save(@RequestBody SpeedOperationTable speedOperationTable){

        List<SpeedOperationTable> projectCode = speedOperationTableService.list(new QueryWrapper<SpeedOperationTable>().eq("project_code", speedOperationTable.getProjectCode()));
        if (projectCode.size() > 0) return R.error("编号："+speedOperationTable.getProjectCode()+"已存在，请勿重复添加！");

        speedOperationTable.setStatus(StateEnum.SUBMIT.getState()).setWordPath("").setPdfPath("");
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        speedOperationTable.setUserId(adminService.getIdByName(authentication.getName()));

		speedOperationTableService.save(speedOperationTable);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("reportapproval:speedoperationtable:update")
    public R update(@RequestBody SpeedOperationTable speedOperationTable){

        speedOperationTableService.updateById(speedOperationTable);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("reportapproval:speedoperationtable:delete")
    public R delete(@RequestBody Integer[] ids){
		speedOperationTableService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    @RequestMapping("/viewPdf/{id}")
    public String viewPdf(@PathVariable("id") Integer id){
        return speedOperationTableService.viewPdf(id);
    }

    @PostMapping("/auditList")
    public R auditList(@RequestBody DataStagingParam param) {
        return R.ok().setData(speedOperationTableService.auditList(param));
    }

    @PostMapping("/review")
    public R review(@RequestBody SpeedOperationTable vegetationRecordForm) {
        speedOperationTableService.updateById(new SpeedOperationTable().setId(vegetationRecordForm.getId()).setStatus(0));
        return R.ok();
    }

    @PostMapping("/audit")
    public R audit(@RequestBody AuditParam auditParam) {
        return speedOperationTableService.audit(auditParam);
    }


    /**
     * 计算平均速度
     * @param pastStr
     * @param antiStr
     * @return
     */
    private static BigDecimal calculateAverageSpeed(String pastStr, String antiStr) {

        BigDecimal past = new BigDecimal(pastStr);
        BigDecimal anti = new BigDecimal(antiStr);

        // 计算平均速度
        BigDecimal averageSpeed = past.add(anti).divide(new BigDecimal("2"), 2, RoundingMode.HALF_UP);

        return averageSpeed;
    }

}
